/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.bl.AutorBlLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Autor;

/**
 *
 * @author Marquez
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
//---------------EJB'S-------------------------

    @EJB
    private AutorBlLocal autorBl;
//------------------variables(propiedades)--------------
    private List<Autor> lista;
    private Autor author;
    private String accion;
//-----------constructor--------------------------------

    public AuthorBean() {
        author = new Autor();
    }
//-----------metodos GET y SET--------------------------------    

    public List<Autor> getLista() {
        lista = autorBl.getAll();
        return lista;
    }

    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }

//-----------Acciones--------------------------------    
    public String procesarAccion() {
        if(accion.equalsIgnoreCase("Nuevo")){
        autorBl.register(author);
        } else if(accion.equalsIgnoreCase("Editar")){
                autorBl.modify(author);
            }
        else if(accion.equalsIgnoreCase("Eliminar")){
                autorBl.delete(author);
           
}
        accion="";
        return "AutorVista";
    }

//-----------Listeners--------------------------------    
    public void prepararNuevo(ActionEvent evt){//accionEvent de faces
        author=new Autor();
        accion="Nuevo";
    }
    
    public void prepararEditar(ActionEvent evt){
        System.out.println(evt.getComponent().getClientId());
        String arr[]=evt.getComponent().getClientId().split(":");
        String num=arr[3];
        int index=Integer.parseInt(num);
        System.out.println("el numero es: "+index);
        author=lista.get(index);
        accion="Editar";
    }
    public void prepararEliminar(ActionEvent evt){
        String arr[]=evt.getComponent().getClientId().split(":");
        String num=arr[3];
        int index=Integer.parseInt(num);
        System.out.println("el numero es: "+index);
        author=lista.get(index);
        accion="Eliminar";
    }
}
